using UnityEngine;
using Voodoo.Pattern;

public class DroneHead : MonoBehaviour
{
	public GameObject DrillFX;

	private Progress progress;

	private GameplayAudio gameplayAudio;

	private GameplayHaptic gameplayHaptic;

	private float rotationSpeed;

	public int amount;

	private MapCreatorEditor _mapGenerator;

	private float _drillEnableTimer;

	private void Start()
	{
		progress = Object.FindObjectOfType<Progress>();
		gameplayAudio = GameObject.Find("GameplaySounds").GetComponent<GameplayAudio>();
		gameplayHaptic = GameObject.Find("Manager").GetComponent<GameplayHaptic>();
		DrillFX.SetActive(value: false);
	}

	public static void AddPhysics(Collider other)
	{
		if (!other.gameObject.TryGetComponent<Rigidbody>(out var component))
		{
			component = other.gameObject.AddComponent<Rigidbody>();
		}
		component.constraints = (RigidbodyConstraints)56;
		component.mass = 0.1f;
		component.isKinematic = false;
		if (other.CompareTag("Gem"))
		{
			other.tag = "GemCollected";
		}
		else
		{
			other.tag = "Player";
		}
		component.AddForce(new Vector3(Random.Range(-1f, 1f), Random.Range(-2f, 0.5f), 0f), ForceMode.Impulse);
	}

	private void OnTriggerStay(Collider other)
	{
		if (!other.CompareTag("Finish") && !other.CompareTag("Gem"))
		{
			return;
		}
		if (_mapGenerator == null)
		{
			_mapGenerator = Object.FindObjectOfType<MapCreatorEditor>();
		}
		int maxExclusive = (_mapGenerator.IsBonusLevel ? 10 : (31 - amount));
		_drillEnableTimer = 1f;
		if (Random.Range(1, maxExclusive) == 1 && (bool)progress)
		{
			AddPhysics(other);
			if (SingletonMB<BonusLevelManager>.Instance.IsBonusLevelActive || Object.FindObjectOfType<MapCreatorEditor>().IsBonusLevel)
			{
				NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) + 1f);
				SingletonMB<BonusFX>.Instance.MoveAndPlay(other.transform.position + new Vector3(0f, 0f, -0.5f));
			}
			progress.KillBlock();
			gameplayAudio.PlaySound(GameplaySoundType.DigSound);
			gameplayHaptic.PlayHaptic(HapticType.Dig);
		}
	}

	private void Update()
	{
		if (_drillEnableTimer > 0f)
		{
			DrillFX.SetActive(value: true);
			_drillEnableTimer -= Time.unscaledDeltaTime;
			if (_drillEnableTimer <= 0f)
			{
				DrillFX.SetActive(value: false);
			}
		}
	}

	private void OnDisable()
	{
		_drillEnableTimer = 0f;
		DrillFX.SetActive(value: false);
	}
}
